package com.yanqu.client;

import com.yanqu.client.player.Player;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.pb.player.HideVipAndAddressProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import io.vertx.core.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Main {

    /**
     * 服务器信息
     */
    public static Map<Integer, ServerInfo> serverInfoMap = new HashMap<Integer, ServerInfo>() {{
        put(1, new ServerInfo("10.1.102.23", 8290, 88800000));
        put(2, new ServerInfo("10.1.102.23", 8291, 88800001));
        put(3, new ServerInfo("10.1.102.23", 8292, 88800002));
        put(4, new ServerInfo("10.1.102.23", 8293, 88800003));
    }};

    /**
     * @param args
     */
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 2; i <= 20; i++) {
            final int index = i;
            executorService.submit(() -> {
                String openId = String.format("%d", index);
                ServerInfo serverInfo = serverInfoMap.get(1);
                Vertx vertx = Vertx.vertx();
                Player player = new Player(serverInfo.getHost(), serverInfo.getPort(), openId, vertx, serverInfo.getServerId());
                Future<Object> gameLogin = player.gameLogin();
                gameLogin.onSuccess(o -> {
                    System.out.println("【模拟】------------------------------------------------登录成功------------------------------------------------");
                    System.out.println("【模拟】openId:" + openId + "用户id:" + player.getUserId());
                    player.sendMessage(jzSign(player, 10012905));
                    player.sendMessage(sendActivityMessage(player, 10012905, "用户:" + openId + ",九州棋局报名成功"));
                    System.out.println("【模拟】执行指令成功");
                }).onComplete(o -> {
                    player.sendMessage(YanQuMessageUtils.buildMessage(GameProtocol.S_PLAYER_LOGOUT, player.getUserId()));
                    System.out.println("【模拟】------------------------------------------------登出成功------------------------------------------------");
                    vertx.close();
                });
            });
        }

        executorService.shutdown();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                // todo 这里关闭连接
                System.out.println("收到关闭事件");
            }
        });
    }

//    public static YanQuMessage enterCricket(Player player) {
//        CricketProto.CricketGetCourtInfoReqMsg.Builder builder = CricketProto.CricketGetCourtInfoReqMsg.newBuilder();
//        return YanQuMessageUtils.buildMessage(GameProtocol.S_CRICKET_GET_COURT_INFO, player.getUserId(), builder);
//    }

    /**
     * 世界聊天
     */
    public static YanQuMessage sendWorldMessage(Player player, String content) {
        WorldMessageProto.WorldChatReqMsg.Builder reqMsg = WorldMessageProto.WorldChatReqMsg.newBuilder();
        reqMsg.setType(1);
        reqMsg.setContent(content);
        return YanQuMessageUtils.buildMessage(GameProtocol.S_WORLD_CHAT, player.getUserId(),reqMsg);
    }


    /**
     * 活动聊天
     */
    public static YanQuMessage sendActivityMessage(Player player, int activityId, String content) {
        WorldMessageProto.WorldChatReqMsg.Builder reqMsg = WorldMessageProto.WorldChatReqMsg.newBuilder();
        reqMsg.setType(15);
        reqMsg.setContentType(1);
        reqMsg.setActivityId(activityId);
        reqMsg.setContent(content);
        return YanQuMessageUtils.buildMessage(GameProtocol.S_WORLD_CHAT, player.getUserId(), reqMsg);
    }

    /**
     * 进入pvp
     */
    public static YanQuMessage enterPVP(Player player) {
        XiuXianProto.XiuXianEnterPvpReqMsg.Builder reqMsg = XiuXianProto.XiuXianEnterPvpReqMsg.newBuilder();
        return YanQuMessageUtils.buildMessage(GameProtocol.S_XIU_XIAN_ENTER_PVP, player.getUserId(),reqMsg);
    }

    /**
     * 进入pvp
     */
    public static YanQuMessage joinUnion(Player player) {
        return YanQuMessageUtils.buildMessage(GameProtocol.S_UNION_JOIN, player.getUserId(), UnionProto.UnionJoinReqMsg.newBuilder().setUnionUid(""));
    }

    /**
     * 报名
     */
    public static YanQuMessage jzSign(Player player, int activityId) {
        return YanQuMessageUtils.buildMessage(GameProtocol.S_JIU_ZHOU_SIGN_UP, player.getUserId(), JiuZhouProto.SignUpJiuZhouReqMsg.newBuilder().setActivityId(activityId));
    }

    public static YanQuMessage settingHideVipAndAddress(Player player) {
        return YanQuMessageUtils.buildMessage(GameProtocol.S_SET_HIDING_VIP_AND_ADDRESS, player.getUserId(),
                HideVipAndAddressProto.HideVipAndAddressReqMsg.newBuilder()
                        .setHide(0)
                        .setType(0)
        );
    }
    static class ServerInfo {

        private String host;

        private int port;

        private int serverId;

        public ServerInfo(String host, int port, int serverId) {
            this.host = host;
            this.port = port;
            this.serverId = serverId;
        }

        public String getHost() {
            return host;
        }

        public int getPort() {
            return port;
        }

        public int getServerId() {
            return serverId;
        }
    }

}